package com.djf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.djf.entity.UserDelivery;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户配送Mapper接口
 * 用户配送表按月分表，与订单表使用相同的分表策略
 *
 * @author djf
 * @since 2025-01-01
 */
@Mapper
public interface UserDeliveryMapper extends BaseMapper<UserDelivery> {

    /**
     * 根据用户ID查询配送记录
     *
     * @param userId 用户ID
     * @return 配送记录列表
     */
    @Select("SELECT * FROM user_delivery WHERE user_id = #{userId} AND deleted = 0 ORDER BY create_time DESC")
    List<UserDelivery> getDeliveriesByUserId(@Param("userId") Long userId);

    /**
     * 根据订单ID查询配送记录
     *
     * @param orderId 订单ID
     * @return 配送记录
     */
    @Select("SELECT * FROM user_delivery WHERE order_id = #{orderId} AND deleted = 0")
    UserDelivery getDeliveryByOrderId(@Param("orderId") Long orderId);

    /**
     * 根据配送单号查询配送记录
     *
     * @param deliveryNo 配送单号
     * @return 配送记录
     */
    @Select("SELECT * FROM user_delivery WHERE delivery_no = #{deliveryNo} AND deleted = 0")
    UserDelivery getDeliveryByDeliveryNo(@Param("deliveryNo") String deliveryNo);

    /**
     * 根据配送状态查询配送记录
     *
     * @param deliveryStatus 配送状态
     * @return 配送记录列表
     */
    @Select("SELECT * FROM user_delivery WHERE delivery_status = #{deliveryStatus} AND deleted = 0 ORDER BY create_time DESC")
    List<UserDelivery> getDeliveriesByStatus(@Param("deliveryStatus") Integer deliveryStatus);

    /**
     * 根据用户ID和时间范围查询配送记录
     *
     * @param userId    用户ID
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 配送记录列表
     */
    @Select("SELECT * FROM user_delivery WHERE user_id = #{userId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} " +
            "AND deleted = 0 ORDER BY create_time DESC")
    List<UserDelivery> getDeliveriesByUserIdAndTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 根据配送状态和时间范围查询配送记录
     *
     * @param deliveryStatus 配送状态
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @return 配送记录列表
     */
    @Select("SELECT * FROM user_delivery WHERE delivery_status = #{deliveryStatus} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} " +
            "AND deleted = 0 ORDER BY create_time DESC")
    List<UserDelivery> getDeliveriesByStatusAndTimeRange(
            @Param("deliveryStatus") Integer deliveryStatus,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 根据订单ID列表批量查询配送记录
     *
     * @param orderIds 订单ID列表
     * @return 配送记录列表
     */
    @Select("<script>" +
            "SELECT * FROM user_delivery WHERE order_id IN " +
            "<foreach collection='orderIds' item='orderId' open='(' separator=',' close=')'>" +
            "#{orderId}" +
            "</foreach>" +
            " AND deleted = 0 ORDER BY create_time DESC" +
            "</script>")
    List<UserDelivery> getDeliveriesByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 根据快递单号查询配送记录
     *
     * @param trackingNumber 快递单号
     * @return 配送记录
     */
    @Select("SELECT * FROM user_delivery WHERE tracking_number = #{trackingNumber} AND deleted = 0")
    UserDelivery getDeliveryByTrackingNumber(@Param("trackingNumber") String trackingNumber);
}